package database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.BeansException;

import database.dao.MachineInfoDao;
import database.dao.UserInfoDao;
import database.pojo.MachineInfo;
import database.pojo.UserInfo;
import util.JdbcUtils;
import util.MyFactory;

public class DBService {
	public UserInfo insertUserInfo(UserInfo userInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).insert(userInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return userInfo;
	}
	
	public int updateAvatarId(int avatarId,String openId) throws BeansException, SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateAvataridByOpenId(avatarId, openId, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	
	
	public UserInfo updateUserInfo(UserInfo userInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).update(userInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return userInfo;
	}
	
	public UserInfo updateExceptLastLoginTime(UserInfo userInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateExceptLastLoginTime(userInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		return userInfo;
	}
	
	public boolean isExistUserInfo(UserInfo userInfo) throws SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).isExist(userInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	public UserInfo selectUserInfoByOpenId(String openId) {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).selectByOpenId(openId, conn);
		}catch (BeansException | SQLException e) {
				e.printStackTrace();	
				return null;
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	public MachineInfo updateMachineInfo(MachineInfo machineInfo) throws BeansException, SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).update(machineInfo, conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	public MachineInfo selectMachineInfoById(int machineId) throws BeansException, SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MachineInfo info =  MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).selectById(machineId, conn);
			
			return info;
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}
	
	public List<MachineInfo> selectAllMachineInfo() throws BeansException, SQLException{
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).selectAll(conn);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public int getUidByOpenId(String openId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).getUidByOpenId(conn,openId);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public int getKCodeByOpenId(String openId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).getKCodeByOpenId(conn,openId);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public void updateKCoin(String openId, int getkCoin) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateKCoin(conn,openId,getkCoin);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public void updateEndtime(int mid, long endTime) {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateEndtime(conn,mid,endTime);
		} catch (BeansException | SQLException e) {
			e.printStackTrace();
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	public void updateUserLogout(UserInfo userInfo) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateUserLogout(conn,userInfo);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	public void updateVipGrade(int vipGrade, String openId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateVipGrade(conn,vipGrade,openId);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	public void updateAllRecharge(double afterAllRecharge, String openId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateAllRecharge(conn,afterAllRecharge,openId);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public void updateAllConsume(double afterAllConsume, String openId) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateAllConsume(conn,afterAllConsume,openId);
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public void updateEndtimeAndAllConsume(String openId, long endTime, double afterAllConsume, int minites) {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateEndtimeAndAllConsume(conn,endTime,afterAllConsume,openId,minites);
		}catch (BeansException | SQLException e) {
			e.printStackTrace();
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	public boolean midISActivate(int machineId) {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).midISActivate(conn,machineId);
			
		}catch (BeansException | SQLException e) {
				e.printStackTrace();
				return false;
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public void updateMidOnline(int machineId, int online) {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).updateMidOnline(conn,machineId,online);
			
		}catch (BeansException | SQLException e) {
				e.printStackTrace();
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	public void updateWinOrDefeat(String winer, String defeater) {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).updateWinOrDefeat(conn,winer,defeater);			
		}catch (BeansException | SQLException e) {
				e.printStackTrace();
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	

	
	public int selectUid(String wechatOpenid) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).selectUid(conn,wechatOpenid);			
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	}

	public int isNewUser(String wechatOpenid) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			return MyFactory.context.getBean("UserInfoDao", UserInfoDao.class).isNewUser(conn,wechatOpenid);			
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
	
	}

	public void updateRoomKey(int machineId, String roomkey) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).updateRoomKey(conn,machineId,roomkey);			
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	public void uploadModelRecord(int machineId, String songId,int endType, int singType, double songPercent, int score) throws BeansException, SQLException {
		Connection conn = null;
		try{
			conn = MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).getConnection();
			MyFactory.context.getBean("MachineInfoDao", MachineInfoDao.class).uploadModelRecord(conn,machineId,songId,endType,singType,songPercent,score);			
		}finally{
			MyFactory.context.getBean("JdbcUtils",JdbcUtils.class).releaseConnection(conn);
		}
		
	}

	
}
